package ligne2vie.topos.client;

import ligne2vie.topos.client.asynccalls.GetAndDisplaySummits;
import ligne2vie.topos.client.maps.MyMapWidget;
import ligne2vie.topos.client.panels.ActionProcessPanel;
import ligne2vie.topos.client.panels.ControlPanel;
import ligne2vie.topos.client.panels.LogInPanel;
import ligne2vie.topos.client.service.SummitService;
import ligne2vie.topos.client.service.SummitServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Index implements EntryPoint {

	private static final int LEFT_PANELS_WIDTH = 310;
	private static final int RIGHT_PANELS_WIDTH = Window.getClientWidth() - LEFT_PANELS_WIDTH - 30;
	private static final int TOP_PANELS_HEIGHT = 60;
	private static final int BOTTOM_PANELS_HEIGHT = Window.getClientHeight() - TOP_PANELS_HEIGHT - 40;

	private final SummitServiceAsync summitService = GWT.create(SummitService.class);

	/**
	 * Creates, sizes and locates all elements to display on the screen.
	 */
	private void assemblyElements() {

		// --- creating an instance of login panel in order to configure its size later

		LogInPanel logInPanel = new LogInPanel();

		VerticalPanel topLeftPanel = new VerticalPanel();
		topLeftPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		topLeftPanel.add(logInPanel);

		// --- creating left panel containing control and action panel

		AbsolutePanel bottomLeftPanel = new AbsolutePanel();
		// bottomLeftPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		bottomLeftPanel.add(ControlPanel.getInstance(), 0, 0);
		bottomLeftPanel.add(ActionProcessPanel.getInstance(), 0, 0);

		// --- creating mapPanel

		DecoratorPanel mapPanel = new DecoratorPanel();
		mapPanel.add(MyMapWidget.getInstance());

		// --- creating main panel

		Grid mainPanel = new Grid(2, 2);
		mainPanel.setWidget(0, 0, topLeftPanel);
		mainPanel.setWidget(1, 0, bottomLeftPanel);
		mainPanel.setWidget(0, 1, new Label("Future SearchBar"));
		mainPanel.setWidget(1, 1, mapPanel);

		// --- configuring size of each element

		mainPanel.setSize(Window.getClientWidth() + "px", Window.getClientHeight() + "px");

		topLeftPanel.setHeight(TOP_PANELS_HEIGHT + "px");
		topLeftPanel.setWidth(LEFT_PANELS_WIDTH + "px");

		logInPanel.setHeight(TOP_PANELS_HEIGHT + "px");
		logInPanel.setWidth(LEFT_PANELS_WIDTH + "px");

		bottomLeftPanel.setWidth(LEFT_PANELS_WIDTH + "px");
		bottomLeftPanel.setHeight(BOTTOM_PANELS_HEIGHT + "px");

		ControlPanel.getInstance().setWidth(LEFT_PANELS_WIDTH + "px");
		ActionProcessPanel.getInstance().setWidth(LEFT_PANELS_WIDTH + "px");

		MyMapWidget.getInstance().setWidth(RIGHT_PANELS_WIDTH + "px");
		MyMapWidget.getInstance().setHeight(BOTTOM_PANELS_HEIGHT + "px");

		// --- adding all elements to window

		RootPanel.get().add(mainPanel);
	}

	/**
	 * Initialize elements at the first application execution.
	 */
	private void initElements() {

		ActionProcessPanel.getInstance().stopAction();
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		assemblyElements();
		initElements();

		// TODO removing or modifying the following process by looking for summits around the map center
		summitService.getAllSummits(new GetAndDisplaySummits());
	}
}
